from django.views.decorators.csrf import csrf_protect
from django.shortcuts import render_to_response, render

import MoodRingPython

@csrf_protect
def index(request):
    """docstring for index"""
    return render(request, 'index.html', {'title': 'test page'})

def upload(request):
    if request.method == 'POST':
        uid = request.POST['uid']
        file_type = request.POST['type']
        f = request.FILES['pic'] # not save to disk
        #f = write_uploaded_file_to_hard_drive(request.FILES['pic']) # save to disk
        res = MoodRingPython.MoodRingMain(uid, file_type, f)
    return render_to_response('upload.html', {'file':f, 'mood_ring': res})

def write_uploaded_file_to_hard_drive(f):
    with open(f.name, 'wb+') as info:
        for chunk in f.chunks():
            info.write(chunk)
    return f